package com.api.gbhg.huangwm.cron;

import com.alibaba.fastjson.JSONObject;
import com.api.gbhg.huangwm.dao.ProjectOperationDao;
import com.api.gbhg.huangwm.dao.ReimbursementListDao;
import com.customization.commons.LogTool;
import com.weaver.general.Util;
import weaver.conn.RecordSet;
import weaver.general.GCONST;
import weaver.interfaces.schedule.BaseCronJob;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Huangwm
 * @Date 2022-06-27
 * @Version 1.0
 * @Description TODO 基于uf_bxjl_kj_dt1表中的数据进行丰富，根据lcid和明细id写入报销核批的渠道和同意报销金额，用于预算报表统计
 **/
public class UpdateKjshRecords extends BaseCronJob {
    private LogTool logTool = new LogTool("/log/gbhg/huangwm/cron/UpdateKjshRecords",false);

    public static void main(String[] args) throws Exception {
        String rootPath = "D:\\WEAVER\\ecology\\";
        GCONST.setRootPath(rootPath);
        GCONST.setServerName("ecology");
        runAfter3();
    }
    @Override
    public void execute() {
        ReimbursementListDao listDao = new ReimbursementListDao();
        listDao.runAfter2();
        try {
            runAfter3();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 同步各流程支付明细中的支出渠道到uf_bxjl_kj_dt2
     */
    public static void runAfter3() throws Exception {
        String upSql = "update uf_bxjl_kj_dt2 set glzfl=?,zflwb=?,skr=?,skfzh=?,skfkhx=? where id=? ";
        ProjectOperationDao dao = new ProjectOperationDao();
        RecordSet rs1 = new RecordSet();
        RecordSet rs2 = new RecordSet();
        RecordSet rs3 = new RecordSet();
        RecordSet rs4 = new RecordSet();
        List<List> datas = new ArrayList<>();
        rs1.execute(" select id,lcid,mxid from uf_bxjl_kj_dt2  WHERE skr IS NULL ");
        while (rs1.next()){
            List data = new ArrayList();
            String dtid = rs1.getString("id");
            String tablename = dao.getWorkflowTabNameByRequestid(rs2,rs1.getString("lcid"));
            if(!tablename.equals("")) {
                String sql = "select dt.id,xzzfl,zflwb,skr,skfzh,skfkhx \n" +
                        "from " + tablename + "_dt2 dt\n" +
                        "where dt.id=" + rs1.getString("mxid");
                boolean r = false;
                if (rs3.execute(sql)) {
                    rs3.next();
                    String xzzfl = com.weaver.general.Util.null2String(rs3.getString("xzzfl"));
                    String zflwb = com.weaver.general.Util.null2String(rs3.getString("zflwb"));
                    String skr = com.weaver.general.Util.null2String(rs3.getString("skr"));
                    String skfzh = com.weaver.general.Util.null2String(rs3.getString("skfzh"));
                    String skfkhx = com.weaver.general.Util.null2String(rs3.getString("skfkhx"));
                    data.add(xzzfl);
                    data.add(zflwb);
                    data.add(skr);
                    data.add(skfzh);
                    data.add(skfkhx);
                    data.add(dtid);
                    datas.add(data);
                    Object[] objects = {
                            xzzfl, zflwb, skr, skfzh, skfkhx, dtid
                    };
                    System.err.println("本次更新参数 " + JSONObject.toJSONString(objects));
                    r = rs4.executeUpdate(upSql, objects);
                } else {
                    sql = "select dt.id,xzzfl,zflwb,skr,skfzh,skfkhx \n" +
                            "from " + tablename + " dt\n" +
                            "where dt.id=" + rs1.getString("mxid");
                    if (rs3.execute(sql)) {
                        rs3.next();
                        String xzzfl = com.weaver.general.Util.null2String(rs3.getString("xzzfl"));
                        String zflwb = com.weaver.general.Util.null2String(rs3.getString("zflwb"));
                        String skr = com.weaver.general.Util.null2String(rs3.getString("skr"));
                        String skfzh = com.weaver.general.Util.null2String(rs3.getString("skfzh"));
                        String skfkhx = Util.null2String(rs3.getString("skfkhx"));
                        data.add(xzzfl);
                        data.add(zflwb);
                        data.add(skr);
                        data.add(skfzh);
                        data.add(skfkhx);
                        data.add(dtid);
                        datas.add(data);
                        Object[] objects = {
                                xzzfl, zflwb, skr, skfzh, skfkhx, dtid
                        };
                        System.err.println("本次更新参数 " + JSONObject.toJSONString(objects));
                        r = rs4.executeUpdate(upSql, objects);
                    }
                }
                if (!r) {
                    System.err.println(sql);
                    throw new Exception("更新异常");
                }
            }
        }

//        try{
//            boolean r = rs4.executeBatchSql(upSql,datas);
//            System.err.println("更新数据结果 "+r);
//        }catch (Exception e){
//            System.err.println("更新数据SQL异常");
//            System.err.println(e.getMessage());
//            e.printStackTrace();
//        }
    }
}
